package com.mingqijia.gassafety.authority.security.valueobject;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mingqijia.gassafety.shared.enums.ManagerType;
import com.mingqijia.gassafety.shared.enums.RunningState;
import com.mingqijia.gassafety.shared.enums.YesNoEnum;
import com.mingqijia.gassafety.shared.user.UserBackend;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Damon S.
 * @version v0.2
 * @date {YEAR}年{MONTH}月{DAY}日 {TIME}
 */
@Data
public class ManagerVO implements UserDetails, UserBackend {
    /** 唯一标识 */
    private String id;
    /** 用户姓名 */
    private String name;
    /** 手机号 */
    private String phone;
    /** 用户名 */
    private String username;
    /** 加密密码 */
    private String password;
    /** 状态：1正常，0冻结 */
    private RunningState state;
    /** 账号类型 */
    private ManagerType type;
    /***/
    @JsonIgnore
    private String token;
    @JsonIgnore
    private Long expireAt;
    /** 所属组织 */
    private List<String> hierarchy;
    /** 部门 */
    private List<String> department;
    /** 所属角色 */
    private List<String> roleIds;
    /** 上次登录的企业ID */
    private String workingSpId;
    /** 描述 */
    private String description;
    /** 最近一次登录时间 */
    private Timestamp loginAt;
    /** 最后登出时间 */
    private Timestamp logoutAt;
    /** 所属组织ID */
    private String spId;
    /** 是否已删除，0否，1是 */
    private Boolean isDeleted;
    /** 是否前置更新密码（1-是，0否） */
    private YesNoEnum forceChangePwd;

    @Override
    @JsonIgnore
    public String getUserId() {
        return getId();
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return isEnabled();
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return !isDeleted && Objects.equals(state, RunningState.RUNNING);
    }
}
